package com.onlineMIS.ORM.entity.headQ.inventoryFlow;

import java.io.Serializable;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.onlineMIS.ORM.DAO.headQ.inventoryFlow.HeadQInventoryStockDAOImpl;
import com.onlineMIS.ORM.DAO.headQ.inventoryFlow.HeadQInventoryStoreDAOImpl;
import com.onlineMIS.ORM.entity.chainS.user.ChainStore;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;


public class HeadqInvenTraceInfoVO implements Serializable {
	private Pattern pattern_cancel = Pattern.compile("^C[A-Z][A-Z][0-9]+");
	private Pattern pattern_normal = Pattern.compile("^[A-Z][A-Z][0-9]+");
	
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 3440313540411506629L;
	/**
     * 货品信息
     */
    private String barcode;
    /**
     * 日期
     */
    private String date;
    private int quantity;
    /**
     * 比如cs, hs, 
     */
    private String actionCode;
    /**
     * 描述,比如 总部进货，总部退货，零售销售，零售退货，调货，
     */
    private String descp;
    /**
     * 单据号
     */
    private String orderId;
    
    /**
     * 库房名称
     */
    private String store;
    
    
    private String year;
    private String brand;
    private String category;
    private String productCode;
    private boolean isDetails;
    private String clientName;
    private double discount;
    
    public HeadqInvenTraceInfoVO(){
    	
    }
    
    public HeadqInvenTraceInfoVO(HeadQInventoryStock stock,String storeName){
    	this.date = Common_util.dateFormat_f.format(stock.getDate());
    	this.quantity = stock.getQuantity();
    	this.store = storeName;
    	this.barcode = stock.getProductBarcode().getBarcode();   	
    	String actionAndId = stock.getOrderId();
    	
    	String[] actionAndIdArray = sliptActionAndId(actionAndId);
    	
    	this.actionCode = actionAndIdArray[0];
    	this.descp = actionAndIdArray[1];
    	this.orderId = actionAndIdArray[2];

    }

    
    public HeadqInvenTraceInfoVO(HeadQInventoryStock stock,ProductBarcode barcode ,String storeName){
    	this.date = Common_util.dateFormat_f.format(stock.getDate());
    	this.quantity = stock.getQuantity();
    	this.store = storeName;
    	this.barcode = stock.getProductBarcode().getBarcode();
    	this.year = barcode.getProduct().getYear().getYear() + barcode.getProduct().getQuarter().getQuarter_Name();
    	this.brand = barcode.getProduct().getBrand().getBrand_Name();
    	this.category = barcode.getProduct().getCategory().getCategory_Name() + barcode.getProduct().getCategoryLevel1().getCategory_Name();
    	if (barcode.getProduct().getProductCode() !=null) {
    		this.productCode =  barcode.getProduct().getProductCode();
		}
    	if (barcode.getColor() !=null) {
    		this.productCode =  barcode.getProduct().getProductCode()+ barcode.getColor().getName();
		}
   	   	
    	String actionAndId = stock.getOrderId();
    	
    	String[] actionAndIdArray = sliptActionAndId(actionAndId);
    	
    	this.actionCode = actionAndIdArray[0];
    	this.descp = actionAndIdArray[1];
    	this.orderId = actionAndIdArray[2];
    	
    	//判断是否是销售单据 true:可以查看单据详情
    	//销售：退货：赠送：(红冲)
		if (actionCode.equals(HeadQInventoryStock.WHOLE_SALES) ||
				actionCode.equals(HeadQInventoryStock.WHOLE_RETURN)  ||
				actionCode.equals( HeadQInventoryStock.WHOLE_FREE)||
				actionCode.equals(HeadQInventoryStock.WHOLESALE_SALES_OFFSET) ||
				actionCode.equals( HeadQInventoryStock.WHOLESALE_RETURN_OFFSET) ||
				actionCode.equals(HeadQInventoryStock.WHOLESALE_FREE_OFFSET) ) {
			this.isDetails = true;
		}
		
    }
    
   
    
	private String[] sliptActionAndId(String actionAndId) {
		String[] actionArray = {"","",""};
		String[] errorArray = {"错误","错误","错误"};
		
		actionAndId = actionAndId.trim();
		
		if (actionAndId.equals("")){
			actionArray[0] = "";
			actionArray[1] = "期初库存";
			actionArray[2] = "";
		} else {
			String actionCode ="" ;
			String orderId = "";
			try {
				Matcher patternCancelMatch = pattern_cancel.matcher(actionAndId);
				Matcher patternNormalMatch = pattern_normal.matcher(actionAndId);
				
				// 截取字母（旧规则）
//				if (patternCancelMatch.find() ||actionAndId.startsWith(HeadQInventoryStock.HEAD_TRANSFER)){
//					actionCode = actionAndId.substring(0,3);
//					orderId = actionAndId.substring(3);
//				} else if (patternNormalMatch.find()){
//					actionCode = actionAndId.substring(0,2);
//					orderId = actionAndId.substring(2);
//				}
				
				// 截取英文字母(新规则）
				char[] actionCharArrray = actionAndId.toCharArray();
				for (int i =0; i < actionCharArrray.length;i++) {
					if(Character.isDigit(actionCharArrray[i])) {
						actionCode = actionAndId.substring(0,i);
						orderId = actionAndId.substring(i);
						break;
					}					
				}
				
				actionArray[0] = actionCode;
				actionArray[1] = Common_util.getHeadqTraceActionDesp(actionCode);
				actionArray[2] = orderId;
			} catch (Exception e) {
				loggerLocal.error(e.getMessage());
				loggerLocal.error(e);
				actionArray = errorArray;
			}
		}
		
		return actionArray;
	}

	
	

	public String getStore() {
		return store;
	}


	public void setStore(String store) {
		this.store = store;
	}


	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}


	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	public String getActionCode() {
		return actionCode;
	}
	public void setActionCode(String actionCode) {
		this.actionCode = actionCode;
	}
	public String getDescp() {
		return descp;
	}
	public void setDescp(String descp) {
		this.descp = descp;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}


	public String getYear() {
		return year;
	}


	public void setYear(String year) {
		this.year = year;
	}


	public String getBrand() {
		return brand;
	}


	public void setBrand(String brand) {
		this.brand = brand;
	}


	public String getCategory() {
		return category;
	}


	public void setCategory(String category) {
		this.category = category;
	}


	public String getProductCode() {
		return productCode;
	}


	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public boolean isDetails() {
		return isDetails;
	}

	public void setDetails(boolean isDetails) {
		this.isDetails = isDetails;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

    
}
